﻿using System;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace MapleLeaf.Client.Controls {

    public class PressButton : Button {

        public int PressTimeOut {
            get => (int)GetValue(PressTimeOutProperty);
            set => SetValue(PressTimeOutProperty, value);
        }


        public int PressInterval {
            get => (int)GetValue(PressIntervalProperty);
            set => SetValue(PressIntervalProperty, value);
        }


        public PressButton() {
            Timer = new Timer();
            Timer.Elapsed += Timer_Elapsed;
            ClickMode = ClickMode.Press;
        }
        private void Timer_Elapsed(object sender, ElapsedEventArgs e) {
            _ = Dispatcher.Invoke(new Action(delegate () {
                Timer.Interval = PressInterval;
                base.OnClick();
            }), new object[0]);
        }
        protected override void OnClick() {
            Timer.Interval = PressTimeOut;
            Timer.Start();
            base.OnClick();
        }
        protected override void OnMouseUp(MouseButtonEventArgs e) {
            Timer.Stop();
            base.OnMouseUp(e);
        }
        static PressButton() {
            PressTimeOutProperty = DependencyProperty.Register("PressTimeOut", typeof(int), typeof(PressButton), new PropertyMetadata(500));
            PressIntervalProperty = DependencyProperty.Register("PressInterval", typeof(int), typeof(PressButton), new PropertyMetadata(100));
        }

        public static readonly DependencyProperty PressTimeOutProperty;

        public static readonly DependencyProperty PressIntervalProperty;

        private Timer Timer;
    }
}
